package com.yunyike.app.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.rop.AbstractInterceptor;
import com.rop.RopRequestContext;
import com.rop.response.Response;
import com.rop.response.Response.Result;
import com.yunyike.app.redis.JedisManager;
import com.yunyike.app.response.ShopperSessionResponse;

@Component
public class UserValidateInterceptor extends AbstractInterceptor {
	
	private Logger log = LoggerFactory.getLogger(UserValidateInterceptor.class);
	@Override
	public void beforeService(RopRequestContext ropRequestContext) {
		log.info("UserValidateInterceptor...");
		String uid = ropRequestContext.getParamValue("shopperId");
		String sessionId = ropRequestContext.getSessionId();
		log.info("UserValidateInterceptor...shopperId="+uid+",sessionId="+sessionId);
		if(uid!=null&&sessionId!=null){
			try {
				ShopperSessionResponse session = (ShopperSessionResponse) JedisManager.getObject(sessionId,ShopperSessionResponse.class);
				if(!uid.equals(session.getShopperId().toString())){
					Response response = new Response();
					Result result = response.getResult();
					result.setMessage("sessionId和导购id不一致，请重新输入");
					result.setErrorCode("555");
					response.setResult(result );
					ropRequestContext.setRopResponse(response );
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
